<html>
<head><meta charset="utf-8"><title>Null in the client code · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html">Null in the client code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="188940957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188940957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188940957">(Feb 24 2020 at 16:22)</a>:</h4>
<p><a href="/user_uploads/4715/DVvbhvq2xATx6picIH7JJvD4/image.png" target="_blank" title="image.png">image.png</a> </p>
<div class="message_inline_image"><a href="/user_uploads/4715/DVvbhvq2xATx6picIH7JJvD4/image.png" target="_blank" title="image.png"><img src="/user_uploads/4715/DVvbhvq2xATx6picIH7JJvD4/image.png"></a></div><p>One of the users got this during the client update.<br>
It got away and updated normally after a few attempts, he reported, so I could not squeeze any extra info out of him unfortunately.</p>
<p>But, IMO, we could process <code>null</code>'s better and show some meaningful message instead.<br>
I have a suspicion that this behavior is caused by this line (simply because it seems to be the only place destructuring <code>releaseName</code>): <a href="https://github.com/rust-analyzer/rust-analyzer/blob/58d44c6ba2de32a31a09bbcaf61365a69b69374c/editors/code/src/installation/download_artifact.ts#L18" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/58d44c6ba2de32a31a09bbcaf61365a69b69374c/editors/code/src/installation/download_artifact.ts#L18">https://github.com/rust-analyzer/rust-analyzer/blob/58d44c6ba2de32a31a09bbcaf61365a69b69374c/editors/code/src/installation/download_artifact.ts#L18</a></p>
<p>which might be caused by the <code>fetchArtifactReleaseInfo</code> function returning <code>null</code> here: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/49b9c8a0524e53f9bd75b50b6e87d7d88587629f/editors/code/src/installation/fetch_artifact_release_info.ts#L38" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/49b9c8a0524e53f9bd75b50b6e87d7d88587629f/editors/code/src/installation/fetch_artifact_release_info.ts#L38">https://github.com/rust-analyzer/rust-analyzer/blob/49b9c8a0524e53f9bd75b50b6e87d7d88587629f/editors/code/src/installation/fetch_artifact_release_info.ts#L38</a> </p>
<p>Or maybe I'm completely wrong and this is an artifact caused by something else.</p>



<a name="188941066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188941066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188941066">(Feb 24 2020 at 16:24)</a>:</h4>
<p><span class="user-mention" data-user-id="231447">@Kirill Bulatov</span> see the "Update the bin server?" thread</p>



<a name="188941258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188941258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188941258">(Feb 24 2020 at 16:25)</a>:</h4>
<p>Interesting, a different field caused the issue here and there. </p>
<p>But glad that it's a single thing we need to just tolerate, thanks for the pointer.</p>



<a name="188941334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188941334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188941334">(Feb 24 2020 at 16:26)</a>:</h4>
<p>I do want an <code>assert()</code> there ...</p>



<a name="188941409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188941409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188941409">(Feb 24 2020 at 16:27)</a>:</h4>
<p>Postfix <code>!</code> is nothing like <code>unwrap</code>, the <code>null</code> error may appear in any unrelated part of your code...</p>



<a name="188941456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188941456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188941456">(Feb 24 2020 at 16:28)</a>:</h4>
<blockquote>
<p>But, IMO, we could process null's better and show some meaningful message instead.</p>
</blockquote>
<p>agree on that</p>



<a name="188941835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188941835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188941835">(Feb 24 2020 at 16:31)</a>:</h4>
<p>Let me clarify, <span class="user-mention" data-user-id="231447">@Kirill Bulatov</span> , you are right in your assumptions. <code>fetchArtifactReleaseInfo()</code> did return <code>null</code>, but we <a href="https://github.com/rust-analyzer/rust-analyzer/blob/bd4ea87f7442541123e3bbd7e17bfecdfb3c18c6/editors/code/src/installation/server.ts#L66" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/bd4ea87f7442541123e3bbd7e17bfecdfb3c18c6/editors/code/src/installation/server.ts#L66">just ignored</a> it with a postfix <code>!</code> operator (which just silences the compiler and does nothing in runtime)</p>
<div class="codehilite"><pre><span></span> <span class="kr">const</span> <span class="nx">releaseInfo</span> <span class="o">=</span> <span class="p">(</span><span class="nx">await</span> <span class="nx">fetchArtifactReleaseInfo</span><span class="p">(</span><span class="nx">source</span><span class="p">.</span><span class="nx">repo</span><span class="p">,</span> <span class="nx">source</span><span class="p">.</span><span class="nx">file</span><span class="p">,</span> <span class="nx">source</span><span class="p">.</span><span class="nx">version</span><span class="p">))</span><span class="o">!</span><span class="p">;</span>
</pre></div>


<p>There was an <code>assert(releaseInfo !== null)</code> before, but we removed it...</p>



<a name="188942324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188942324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188942324">(Feb 24 2020 at 16:36)</a>:</h4>
<p>the main reason to remvoe that assert was that it pulled in a 3rd party lib. Sorry if didn't clarified that initially, I'll be totally ok with adding</p>
<div class="codehilite"><pre><span></span>function checkNonNull&lt;T&gt;(value: T | undefined): T
</pre></div>


<p>function to utils.ts</p>



<a name="188942416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188942416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188942416">(Feb 24 2020 at 16:37)</a>:</h4>
<p>Thanks for the clarification.</p>
<p>It's not like you've asked for my opinion, but here it is anyway <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span> <br>
I would have used neither <code>!</code> nor <code>assert</code> and did a nullability check instead, seems like a logical way to be happy both with the compiler and with the unknown disturbances in the network, that may cause the function return <code>null</code> (if I got it right, we return <code>null</code> in the function if we fail to get the release via the network, seems like a possible enough usecase to process it instead of silencing).</p>



<a name="188942489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188942489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188942489">(Feb 24 2020 at 16:38)</a>:</h4>
<p>Can we show a message instead of the assert? I don't expect the LSP server name to change often, but it's data coming from GitHub and it's reasonable to handle errors and such.</p>



<a name="188942898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188942898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188942898">(Feb 24 2020 at 16:42)</a>:</h4>
<p>The function returns <code>null</code> only when the request was successful but the artifact was not found, otherwise all network errors are considered unrecoverable and are just caught by the <code>catch</code> clause <a href="https://github.com/rust-analyzer/rust-analyzer/blob/bd4ea87f7442541123e3bbd7e17bfecdfb3c18c6/editors/code/src/installation/server.ts#L70-L89" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/bd4ea87f7442541123e3bbd7e17bfecdfb3c18c6/editors/code/src/installation/server.ts#L70-L89">here</a>, an assertion error would be caught too.</p>
<div class="codehilite"><pre><span></span> <span class="k">catch</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">vscode</span><span class="p">.</span><span class="nb">window</span><span class="p">.</span><span class="nx">showErrorMessage</span><span class="p">(</span>
            <span class="sb">`Failed to download language server from </span><span class="si">${</span><span class="nx">source</span><span class="p">.</span><span class="nx">repo</span><span class="p">.</span><span class="nx">name</span><span class="si">}</span><span class="sb"> `</span> <span class="o">+</span>
            <span class="sb">`GitHub repository: </span><span class="si">${</span><span class="nx">err</span><span class="p">.</span><span class="nx">message</span><span class="si">}</span><span class="sb">`</span>
        <span class="p">);</span>

        <span class="nx">log</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>

        <span class="nx">dns</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="s1">&#39;example.com&#39;</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span>
            <span class="nx">addrs</span> <span class="o">=&gt;</span> <span class="nx">log</span><span class="p">.</span><span class="nx">debug</span><span class="p">(</span><span class="s2">&quot;DNS resolution for example.com was successful&quot;</span><span class="p">,</span> <span class="nx">addrs</span><span class="p">),</span>
            <span class="nx">err</span> <span class="o">=&gt;</span> <span class="p">{</span>
                <span class="nx">log</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span>
                    <span class="s2">&quot;DNS resolution for example.com failed, &quot;</span> <span class="o">+</span>
                    <span class="s2">&quot;there might be an issue with Internet availability&quot;</span>
                <span class="p">);</span>
                <span class="nx">log</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">);</span>
        <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
    <span class="p">}</span>
</pre></div>



<a name="188943121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Null%20in%20the%20client%20code/near/188943121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Null.20in.20the.20client.20code.html#188943121">(Feb 24 2020 at 16:44)</a>:</h4>
<p>Another approach would be to make <code>featchArtifactReleaseInfo()</code> return non-nullable value and throw <code>Artifact was not found</code> error instead of returning <code>null</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>